﻿using MediatR;
using RBAC.Common.Domains.RabbitProject;
using RBAC.Common.Dto;
using RBAC.Common.Dto.ProSorce;
using RBAC.Common.infrastructures.interfaces;
using RBAC.Enum;

namespace RBAC.api.Application.commond.Slaughter
{
    public class SlaughterinformationdisplayHtmlHandler : IRequestHandler<SlaughterinformationdisplayHtmlCommond, Rulst>
    {

        private readonly IbaseRpository<slaughterMess> _slaughtermess;

        private readonly IbaseRpository<ProductionPutImages> _ProductionPutImages;
        private readonly IbaseRpository<City> _City;
        private readonly IbaseRpository<county> _county;
        private readonly IbaseRpository<Province> _Province;

        public SlaughterinformationdisplayHtmlHandler(IbaseRpository<slaughterMess> slaughtermess, IbaseRpository<ProductionPutImages> productionPutImages, IbaseRpository<City> city, IbaseRpository<county> county, IbaseRpository<Province> province)
        {
            _slaughtermess = slaughtermess;
            _ProductionPutImages = productionPutImages;
            _City = city;
            _county = county;
            _Province = province;
        }

        public async Task<Rulst> Handle(SlaughterinformationdisplayHtmlCommond request, CancellationToken cancellationToken)
        {

            var query = new Rulst() { Code = (int)Reult.success };


            var list = (from a in _slaughtermess.Query()
                       where a.ProDuctionBasicid == request.ProDuctionBasicid
                       select new RelevantqualifiedenterprisesDto()
                       {
                        
                           SlaugherComeTime = a.SlaugherComeTime,
                           SlaugherTime = a.SlaugherTime,
                           SlaugherWeight = a.SlaugherWeight,
                           Slaughterlot = a.Slaughterlot,
                           Slaughteringenterprise = new List<Slaughteringenterprise>()
                           ,
                           correlation = new List<correlation>(),
                            Newcity=new List<NEWCity>(),

                           
                       }).FirstOrDefault();


            if(list!=null)
            {

                list.Newcity = (from a in _slaughtermess.Query()
                               join b in _City.Query() on a.Cityid equals b.Cityid
                               join c in _county.Query() on a.countyid equals c.Coutnyid
                               join t in _Province.Query() on a.Provinceid equals t.ProvinceId
                               select new NEWCity()
                               {

                                   SetupTime = a.SetupTime,
                                   BossIntroMess = a.BossIntroMess,
                                   BossName = a.BossName,
                                   Cityname = b.CityName,   
                                   countyname = c.CountyName,
                                   Provincename = t.ProvinceName


                               }).ToList();


                //相关资质照片
                list.Slaughteringenterprise = _ProductionPutImages.Query().Where(x => x.ProDuctionBasicid == request.ProDuctionBasicid && x.Stypeid == Typenum.相关资质).Select(x =>new Slaughteringenterprise
                {
                    CoreImages=x.PutImages,
                }).ToList();

                //相关资质照片
                list.correlation = _ProductionPutImages.Query().Where(x => x.ProDuctionBasicid == request.ProDuctionBasicid && x.Stypeid == Typenum.屠宰企业).Select(x => new correlation
                {
                    CoreImages = x.PutImages,
                }).ToList();



            }
            else
            {
                list = new RelevantqualifiedenterprisesDto();

            }



            query.data = list;

            return query;


        }
    }
}
